import Gobj from "./Gobj";
import GUniverse from "./GUniverse";
import GEnemy from "./GEnemy";

export default class GBullet extends Gobj{
    constructor(){
        super();
        this.type = Gobj.GBullet;
        //this.speed = 20 * 100;
        this.speed = 20;
        this.hp = 1;                
    }

    get eid(){
        return this.v0;
    }

    set eid(eid_:number){
        this.v0 = eid_;
    }

    get tx(){
        return this.v1;
    }

    set tx(tx_:number){
        this.v1 = tx_;
    }

    get ty(){
        return this.v2;
    }

    set ty(ty_:number){
        this.v2 = ty_;
    }

    get EnemeyKey(){
        return this.pid + "_" + Gobj.GEnemy + "_" + this.v0;
    }

    copy():Gobj{
        var o = new GBullet();
        o.syncTime(this);
        o.id = this.id;
        return o;
    }

    timeline(sf:number , lf:number , fms:number , keyf:boolean , u:GUniverse, ac:any){
        if(fms>this.st){

            var player = u.gm.getPlayer(this.pid);
            var e = player.monsterMap.getObj( this.EnemeyKey ) as GEnemy;
            if(e && !e.die){
                this.z += this.speed;
                this.tx = Math.floor(e.x/100);
                this.ty = Math.floor(e.y/100);
                var xx =  this.tx - this.x ;
                var yy =  this.ty - this.y ;
                var dd = (xx*xx + yy*yy);
                if( this.z*this.z >=  dd){
                    this.z = Math.sqrt(dd);
                    this.hp = 0;
                    player.bulletMap.removeobj(this.key);

                    e.hp -= 20 * 100;
                    if(e.hp<0)e.hp = 0;
                    if(e.hp<100){
                        e.die = true;
                        player.monsterMap.removeobj(e.key);
                        player.bulletMap.removeobj(this.key);
                    }
                }
                //console.log(this.z);
                
                // var r = Math.atan2(yy, xx);
                // Math.cos(r)*this.z;
                // Math.sin(r)*this.z;
            }else{
                player.bulletMap.removeobj(this.key);
            }
        }
    }





}